package com.hndfsj.app.datav;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSON;
import com.hndfsj.framework.utils.properties.PropertiesUtil;

@Controller
@RequestMapping("/jdyw")
public class JdywController extends DataVBaseController {
	static Logger log = LoggerFactory.getLogger(JdywController.class);
	public static final String PROJECT_URL = PropertiesUtil.getString("project.jdyw.url");

	/**
	 * 概况总数
	 * 
	 * @param type
	 *            [es设备总数,efs设备故障总数,fs报修总数,fc报修完成率,rav平均维修时长]
	 * @return
	 * @throws Exception
	 *             zxj
	 */
	@RequestMapping(value = "/equip/name/value")
	public void dayCarFlow(@RequestParam String type, HttpServletResponse response) {
		String url = PROJECT_URL + "/rest/common/statistics/name/value";
		String result = getJsonData(url);
		if (StringUtils.isBlank(result) || request(url, 60 * 60 * 1)) {
			result = parseServerData2JsonString(url);
			putCache(url, result);
		}
		writeJson(response, "[" + JSON.parseObject(result).getString(type) + "]");
	}

	/**
	 * 十大故障原因
	 * 
	 * @param type
	 * @param response
	 *            zxj
	 */
	@RequestMapping(value = "/equip/fault/reason")
	public void equipFaultReason(HttpServletResponse response) {
		// String url =PROJECT_URL+ "/rest/equip/fault/reason";
		String url = "/equip/fault/reason";
		String result = getJsonData(url);
		if (StringUtils.isBlank(result) || request(url, 60 * 60 * 1)) {
			// result = parseServerData2JsonString(url);
			result = getData(url);
			putCache(url, result);
		}
		writeJson(response, JSON.parseArray(result).toJSONString());
	}

	/**
	 * 报修热频词云
	 * 
	 * @param type
	 * @param response
	 *            zxj
	 */
	@RequestMapping(value = "/fault/word/cloud")
	public void faultWordCloud(HttpServletResponse response) {
		String url = PROJECT_URL + "/rest/common/statistics/fault/word/cloud";
		String result = getJsonData(url);
		if (StringUtils.isBlank(result) || request(url, 60 * 60 * 1)) {
			result = parseServerData2JsonString(url, "respList");
			putCache(url, result);
		}
		writeJson(response, JSON.parseArray(result).toJSONString());
	}

	/**
	 * 设备分布
	 * 
	 * @param type
	 *            收费站 服务区
	 * @param response
	 *            zxj
	 */
	@RequestMapping(value = "/site/equip/count")
	public void siteEquipCount(@RequestParam String type, HttpServletResponse response) {
		String url = PROJECT_URL + "/rest/common/statistics/site/equip/count";
		String result = getJsonData(url);
		if (StringUtils.isBlank(result) || request(url, 60 * 60 * 1)) {
			result = parseServerData2JsonString(url);
			putCache(url, result);
		}
		writeJson(response, JSON.parseObject(result).getString(type));
	}

	@RequestMapping(value = "/repair/use/time")
	public void repairUseTime(HttpServletResponse response) {
		String url = PROJECT_URL + "/rest/common/statistics/repair/use/time";
		String result = getJsonData(url);
		if (StringUtils.isBlank(result) || request(url, 60 * 60 * 1)) {
			result = parseServerData2JsonString(url, "respList");
			putCache(url, result);
		}
		writeJson(response, JSON.parseArray(result).toJSONString());
	}

	/**
	 * 一年维修趋势
	 * 
	 * @param response
	 *            zxj
	 */
	@RequestMapping(value = "/repair/year/trend")
	public void repairYearTrend(HttpServletResponse response) {
		String url = PROJECT_URL + "/rest/common/statistics/repair/year/trend";
		String result = getJsonData(url);
		if (StringUtils.isBlank(result) || request(url, 60 * 60 * 1)) {
			result = parseServerData2JsonString(url, "respList");
			putCache(url, result);
		}
		writeJson(response, JSON.parseArray(result).toJSONString());
	}

	@RequestMapping(value = "/immediate/message")
	public void immediateMessage(HttpServletResponse response) {
		String url = PROJECT_URL + "/rest/common/statistics/immediate/message";
		String result = getJsonData(url);
		if (StringUtils.isBlank(result) || request(url, 60)) {
			result = parseServerData2JsonString(url, "respList");
			putCache(url, result);
		}
		writeJson(response, JSON.parseArray(result).toJSONString());
	}
}
